//ST表(Balanced Lineup G-两个ST表同时维护区间最大最小值)
#include <iostream>
using namespace std;
const int N = 5e4 + 10;
int n, q;
int a[N];
int f1[N][25], f2[N][25];
int lg[N];

void init()
{
    lg[0] = -1;
    for(int i = 1; i <= n; i++)
    {
        lg[i] = lg[i / 2] + 1;
        f1[i][0] = f2[i][0] = a[i];
    }

    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 1; i + (1 << j) - 1 <= n; i++)
        {
            f1[i][j] = max(f1[i][j-1], f1[i + (1 << (j - 1))][j-1]);
            f2[i][j] = min(f2[i][j-1], f2[i + (1 << (j - 1))][j-1]);
        }   
    }
}

int query(int l, int r)
{
    int k = lg[r - l + 1];
    int _max = max(f1[l][k], f1[r - (1 << k) + 1][k]);
    int _min = min(f2[l][k], f2[r - (1 << k) + 1][k]);
    return _max - _min;
}

int main()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> a[i];
    init();
    int l, r; 
    while(q--)
    {
        cin >> l >> r;
        cout << query(l, r) << endl;
    }
    return 0;
}